package com.example.hrsystem.data.repository;

import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.hrsystem.data.model.AttendanceRecord;
import com.example.hrsystem.data.source.AppDatabase;
import com.example.hrsystem.data.source.AttendanceDao;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 打卡记录仓库类
 * 处理打卡相关的数据业务逻辑
 */
public class AttendanceRepository {

    private AttendanceDao attendanceDao;
    private LiveData<List<AttendanceRecord>> allAttendanceRecords;

    /**
     * 构造方法
     */
    public AttendanceRepository(Application application) {
        AppDatabase database = AppDatabase.getDatabase(application);
        attendanceDao = database.attendanceDao();
        allAttendanceRecords = attendanceDao.getAllAttendanceRecords();
    }

    /**
     * 获取所有打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAllAttendanceRecords() {
        return allAttendanceRecords;
    }

    /**
     * 根据员工ID获取打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAttendanceRecordsByEmployeeId(String employeeId) {
        return attendanceDao.getAttendanceRecordsByEmployeeId(employeeId);
    }

    /**
     * 根据日期获取打卡记录
     */
    public LiveData<AttendanceRecord> getAttendanceRecordByDate(Date date, String employeeId) {
        return attendanceDao.getAttendanceRecordByDate(date, employeeId);
    }

    /**
     * 获取指定日期范围内的打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAttendanceRecordsByDateRange(Date startDate, Date endDate, String employeeId) {
        return attendanceDao.getAttendanceRecordsByDateRange(startDate, endDate, employeeId);
    }

    /**
     * 获取未完成打卡的记录
     */
    public LiveData<List<AttendanceRecord>> getUncompletedAttendanceRecords(String employeeId) {
        return attendanceDao.getUncompletedAttendanceRecords(employeeId);
    }

    /**
     * 获取今日签到记录
     */
    public LiveData<AttendanceRecord> getTodayAttendanceRecord(String employeeId) {
        return attendanceDao.getTodayAttendanceRecord(employeeId);
    }

    /**
     * 插入一条打卡记录
     * 使用异步任务执行数据库操作
     */
    public void insert(AttendanceRecord record) {
        try {
            if (record == null || attendanceDao == null) {
                return;
            }
            new InsertAttendanceAsyncTask(attendanceDao).execute(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新打卡记录
     * 使用异步任务执行数据库操作
     */
    public void update(AttendanceRecord record) {
        try {
            if (record == null || attendanceDao == null) {
                return;
            }
            new UpdateAttendanceAsyncTask(attendanceDao).execute(record);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 异步插入打卡记录的任务
     */
    private static class InsertAttendanceAsyncTask extends AsyncTask<AttendanceRecord, Void, Void> {

        private AttendanceDao attendanceDao;

        private InsertAttendanceAsyncTask(AttendanceDao attendanceDao) {
            this.attendanceDao = attendanceDao;
        }

        @Override
        protected Void doInBackground(AttendanceRecord... records) {
            try {
                if (attendanceDao != null && records != null && records.length > 0 && records[0] != null) {
                    attendanceDao.insert(records[0]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    /**
     * 异步更新打卡记录的任务
     */
    private static class UpdateAttendanceAsyncTask extends AsyncTask<AttendanceRecord, Void, Void> {

        private AttendanceDao attendanceDao;

        private UpdateAttendanceAsyncTask(AttendanceDao attendanceDao) {
            this.attendanceDao = attendanceDao;
        }

        @Override
        protected Void doInBackground(AttendanceRecord... records) {
            try {
                if (attendanceDao != null && records != null && records.length > 0 && records[0] != null) {
                    attendanceDao.update(records[0]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    /**
     * 获取同步的今日打卡记录
     * 用于需要立即获取结果的场景
     */
    public AttendanceRecord getTodayAttendanceRecordSync(String employeeId) {
        try {
            return new GetTodayAttendanceSyncTask(attendanceDao).execute(employeeId).get();
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 同步获取今日打卡记录的任务
     */
    private static class GetTodayAttendanceSyncTask extends AsyncTask<String, Void, AttendanceRecord> {

        private AttendanceDao attendanceDao;

        private GetTodayAttendanceSyncTask(AttendanceDao attendanceDao) {
            this.attendanceDao = attendanceDao;
        }

        @Override
        protected AttendanceRecord doInBackground(String... params) {
            try {
                if (attendanceDao != null && params != null && params.length > 0) {
                    // 使用同步方法获取数据
                    // 注意：这里需要在AppDatabase中添加同步方法
                    // 由于LiveData不支持同步获取，这里返回null
                    // 实际实现中需要在AttendanceDao中添加同步查询方法
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}